<?php
namespace api\common\model;
use think\Db;
class Viporder extends \think\Model 
{
    protected $autoWriteTimestamp = true;
    protected $table = 'vip_order';

    /**
     * 创建vip订单
     */
    public function create_vip_order($userid,$amount,$duration,$vipid,$staffId,$regionId,$vipNum){
        $res = false;
        $ordernumber = time().'1'.$userid.random(2,1);
        $data['orderNumber'] = $ordernumber;
        $data['userId'] = $userid;
        if($staffId){
            $data['staffId'] = $staffId;
        }
        $date = date('Y-m-d H:i:s');
        $data['amount'] = $amount;
        $data['addtime'] =$date; 
        $data['duration'] = $duration;
        $data['vipid'] = $vipid;
        $data['vipNum'] = $vipNum;
        $data['status'] = '1';
        $data['updateTime'] = $date;
        $data['regionId'] = $regionId;
        $insert = Db::name($this->table)->insert($data);
        if($insert){
            $res = ['oderNumber'=>$ordernumber];
        }
        return $res;
    }

    /**
     * 根据订单号获取订单信息
     */
    public function vip_order_info($orderNumber){

        $res =Db::name($this->table)->where('orderNumber',$orderNumber)->find();
        return $res;
    }


    /**
     * 根据订单号修改订单
     */
    public function vip_order_update($orderNumber,$status,$wxorder,$userData,$regionjc){
        $date = date('Y-m-d H:i:s');
        $data['status'] = $status;
        $data['updateTime'] = $date;
        $data['wxOrder'] = $wxorder;
        Db::startTrans();
        $transStatus = false;
        try{
            // 修改vip订单
            $updVip = Db::name('vip_order')->where('orderNumber', $orderNumber)->update($data);
            if (!$updVip) {
                setLog('vip订单修改失败','orderPay');
                throw new \Exception("vip订单修改失败");
            }
            // 修改用户状态
            $userData['updateTime'] = $date;
            $userId = $userData['userId'];
            $user = new User();

            $isNum = $user->getInfoById($userId);
            if($isNum){
                $userNumber = $isNum['userNumber'];
                if($userNumber && count($userNumber)>0){
                }else{ 
                    $number = $user->selNumber();
                    $userData['vipCard'] = card_rule($regionjc,$number);
                    $userData['userNumber'] = $number;
                }
            }else{
                setLog('查询用户信息失败','orderPay'); 
            }
            $user = Db::name('users')->where('userId',$userId)->update($userData);
            if (!$user) {
                setLog('用户状态修改失败','orderPay');
                throw new \Exception("用户状态修改失败");
            }
            $transStatus = true;
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            \think\Log::record($e->getMessage());
            // 回滚事务
            Db::rollback();
        }
        return $transStatus;
    }
}
